
/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using System.Collections;

namespace EXtremecode.BusinessLayer
{
	/// <summary>
	/// Summary description for TypeHash.
	/// </summary>
	public class BusinessObjectRelation
	{
		private string m_strName;
		private string[] m_strsReferencedField;
		private string m_strReferencedTable;
		private string[] m_strsNativeField;
		private string m_strNativeTable;
		private Hashtable htNativeRefKeysValues=new Hashtable();
		private Hashtable htRefNativeKeysValues=new Hashtable();
		
		
		
		public BusinessObjectRelation(string name,string strNativeTable,string[] strsNativeField,string strReferencedTable,string[] strsReferencedField)
		{
			m_strName=name;
			m_strsReferencedField=strsReferencedField;
			m_strReferencedTable=strReferencedTable ;
			m_strNativeTable=strNativeTable;
			m_strsNativeField=strsNativeField;
			for(int i=0; i<strsNativeField.Length; i++) 
			{
				htNativeRefKeysValues.Add(strsNativeField[i],strsReferencedField[i]);
				htRefNativeKeysValues.Add(strsReferencedField[i],strsNativeField[i]);
			}


		}

		public string Name
		{
			get
			{
				return m_strName; 
			}
		}
		public string[] ReferencedFields
		{
			get
			{
				return m_strsReferencedField; 
			}
		}
		public string ReferencedTable
		{
			get
			{
				return m_strReferencedTable; 
			}
		}
		public string NativeTable
		{
			get
			{
				return m_strNativeTable; 
			}
		}
		public string[] NativeFields
		{
			get
			{
				return m_strsNativeField; 
			}
		}
		public string GetReferencedField(string nativeField)
		{
			if(!htNativeRefKeysValues.Contains(nativeField))
				throw new Exception(string.Format("Invalid native field '{0}'",nativeField));
			return htNativeRefKeysValues[nativeField] as string;
		}
		public string GetNativeField(string referencedField)
		{
			if(!htRefNativeKeysValues.Contains(referencedField))
				throw new Exception(string.Format("Invalid referenced field '{0}'",referencedField));
			return htRefNativeKeysValues[referencedField] as string;
		}
		
	}
	public class BusinessObjectRelationCollection:Hashtable
	{
		
		
		public BusinessObjectRelation this[string referencedTable]
		{
			get
			{
				return (base[referencedTable] as BusinessObjectRelation);
				
			}
			
		}
		public  void Add(string referencedTable, BusinessObjectRelation value)
		{
			if(base.Contains(referencedTable))throw new Exception(string.Format("[UE] BusinessObjectRelation with the key '{0}', already exsists",referencedTable));
			base.Add(referencedTable,value);
		}
		
		
		
		#region Hidden Methods
		public override void Add(object key, object value)
		{
			throw new Exception("[UE] method is not allowed");
		}
		public override void CopyTo(Array array, int arrayIndex)
		{
			throw new Exception("[UE] method is not allowed");

		}
		#endregion
	}
}
